package com.caiyouxi.user.util.message;

import com.alibaba.fastjson.JSONObject;
import com.caiyouxi.util.HttpUtil;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/20.
 */
public class MsgUtil {
    protected static final Log log = Logs.get();

    public static Boolean sentMsg(SendSms s){

        if(Strings.isEmpty(s.getCmd())){
            log.error("sendSms error: Cmd为空");
            return false;
        }

        if(Strings.isEmpty(s.getUserName())){
            log.error("sendSms error: UserName为空");
            return false;
        }

        if (Strings.isEmpty(s.getPassWord())){
            log.error("sendSms error: PassWord为空");
            return false;
        }

        if (!Strings.isMobile(s.getMobilePhone())){
            log.error("sendSms error: PhoneNumber格式不正确");
            return false;
        }

        if(Strings.isEmpty(s.getBody())){
            log.error("sendSms error: Body为空");
            return false;
        }

        Map<String, Object> parms = new HashMap<>();

        parms.put("cmd", s.getCmd());
        parms.put("userName", s.getUserName());
        parms.put("passWord", s.getPassWord());
        parms.put("mobilePhone", s.getMobilePhone());
        parms.put("body", s.getBody());

        String tmp = HttpUtil.post(s.getPath(), parms);
        System.out.println(tmp);

        JSONObject jsonObject =  JSONObject.parseObject(tmp);

        String flag  =  (String )jsonObject.get("resultCode");
        if ("0".equals(flag)){
            return true;
        }

        return false;
    }

}
